package org.wanho.rent.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import org.wanho.rent.constant.Constant;
import org.wanho.rent.entity.Company;
import org.wanho.rent.entity.LeadingPeriod;
import org.wanho.rent.entity.News;
import org.wanho.rent.entity.Product;
import org.wanho.rent.entity.ProductType;
import org.wanho.rent.entity.SysUser;
import org.wanho.rent.factory.ObjectFactory;
import org.wanho.rent.service.ProductService;
import org.wanho.rent.util.AjaxResult;
import org.wanho.rent.util.PageInfo;
import org.wanho.rent.util.ResponseUtil;

/**
 * 产品控制器
 * @author student
 *
 */
@WebServlet(urlPatterns = "*.product")
public class ProductController extends HttpServlet {
	
	private ProductService productServiceProxy = (ProductService) ObjectFactory.getObject("productServiceProxy");
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println(request.getServletPath() + "###");
		String servletPath = request.getServletPath();
		if (servletPath.equals("/addproduct.product")) {
			addProduct(request, response);
		} else if (servletPath.equals("/updateProduct.product")) {
			updateProduct(request, response);
		} else if (servletPath.equals("/selectAll.product")) {
			selectAllProductByPage(request, response);
		} else if (servletPath.equals("/deleteProduct.product")) {
			deleteProduct(request, response);
		} else if (servletPath.equals("/selectProductById.product")) {
			selectById(request, response);
		}

	}

	/**
	 * 根据id查询一个产品
	 * @param request
	 * @param response
	 */
	private void selectById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String productId = request.getParameter("productId");
		System.out.println(productId + "=proId");

		Product product = new Product(3, "人人贷", 0.01, 0.04, 1, 400, 500, 1, 1, "张二", "中国银行3",
				"行南京分行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯的", new Date());
		ProductType productType = new ProductType(2, "类型4", 1);
		LeadingPeriod period = new LeadingPeriod(3, "3个月", 1);
		Company company = new Company(1, "中国银行", "中国银行是国有银行，中国银行是国有银行中...", 0.03, "/1.jpg");
		product.setPeriod(period);
		product.setCompany(company);
		product.setProductType(productType);
		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", product, true, response);

	}

	/**
	 * 根据产品ID删除已有的产品
	 * @param request
	 * @param response
	 */
	private void deleteProduct(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String productId = request.getParameter("productId");
		System.out.println("productId=" + productId);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);

	}

	/**
	 * 分页查询所有产品(后台)(包括前台页面的多条件查询)
	 * @param request
	 * @param response
	 */
	private void selectAllProductByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 如果是前台页面发送的请求则带着用户点击的参数进行分页
		String productTypeParam = request.getParameter("productType");
		String productDuration = request.getParameter("productDuration");
		String productRate = request.getParameter("productRate");
		String productAmount = request.getParameter("productAmount");
		System.out.println("productTypeParam=" + productTypeParam + " productDuration=" + productDuration
				+ " productRate=" + productRate + " productAmount=" + productAmount);

		// 后台是不带任何查询条件的分页
		String pageNo = request.getParameter("pageNo");
		System.out.println(pageNo + "#");
		int page = Integer.parseInt(pageNo);
		
		PageInfo<Product> pageInfo = productServiceProxy.findByPage(productTypeParam, productDuration, productRate, productAmount, page, Constant.DEFAULT_PAGE_SIZE);

		ResponseUtil.doResponse("成功！",pageInfo, true,response);

	}

	/**
	 * 修改已有的产品信息
	 * @param request
	 * @param response
	 */
	private void updateProduct(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String productId = request.getParameter("productId");
		String productName = request.getParameter("productName");
		String minProductRate = request.getParameter("minProductRate");

		String maxProductRate = request.getParameter("maxProductRate");
		String companyId = request.getParameter("company");
		String minLoansize = request.getParameter("minLoansize");
		String maxLoansize = request.getParameter("maxLoansize");
		String productTypeId = request.getParameter("productType");
		String productDurationId = request.getParameter("productDuration");
		String linkman = request.getParameter("linkman");
		String bank = request.getParameter("bank");
		String content = request.getParameter("content");
		System.out.println(productId + "=id" + productName + "=pname" + minProductRate + "=minrate" + maxProductRate
				+ "maxrate, comId=" + companyId + "" + minLoansize + "= minSize, maxSize=" + maxLoansize + " ptId="
				+ productTypeId + " perId=" + productDurationId + " linkman=" + linkman + "" + "bank=" + bank
				+ " content=" + content);

	}

	/**
	 * 添加新的产品
	 * @param request
	 * @param response
	 */
	private void addProduct(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String productName = request.getParameter("productName");
		String minProductRate = request.getParameter("minProductRate");

		String maxProductRate = request.getParameter("maxProductRate");
		String companyId = request.getParameter("company");
		String minLoansize = request.getParameter("minLoansize");
		String maxLoansize = request.getParameter("maxLoansize");
		String productTypeId = request.getParameter("productType");
		String productDurationId = request.getParameter("productDuration");
		String linkman = request.getParameter("linkman");
		String bank = request.getParameter("bank");
		String content = request.getParameter("content");
		System.out.println(productName + "" + minProductRate + "" + maxProductRate + " comId" + companyId + ""
				+ minLoansize + "" + maxLoansize + " ptId" + productTypeId + " perId" + productDurationId + "" + linkman
				+ "" + "" + bank + "" + content);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);
	}
}
